package xyz.syyrjx.blog.entity.ticTacToe;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/4/25 19:25
 * @description 房间类
 * @since v0.0.1
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Room {

    // 房间id
    private String id;

    // 房间类型1-大小棋；2-无尽
    private int type;

    // 棋盘
    private int[][] board = new int[3][3];

    // 玩家
    private Gamer[] gamers = new Gamer[2];

    // 房间状态-2无人准备 -1一人准备 0游戏进行中 1-1p赢，2-2p赢
    private int state = -2;

    public void init() {
        board = new int[3][3];
    }

    public void removeLastBig(Gamer gamer) {
        // 删去最后一个
        int[][] big = gamer.getBig();
        if (big[2][0] == -1 || big[2][1] == -1) {
            return;
        }
        board[big[2][0]][big[2][1]]  %= 10;
    }

    public void judge() {
        judgingVector();
        if (this.state > 0) {
            for (Gamer gamer : gamers) {
                gamer.init();
            }
        }
    }
    private void judgingVector(){
        for (int i = 0; i < 3; i++) {
            this.state = judgingSame(board[i][0], board[i][1], board[i][2]);
            if (state != 0) {
                return;
            }
            this.state = judgingSame(board[0][i], board[1][i], board[2][i]);
            if (state != 0) {
                return;
            }
        }
        this.state = judgingSame(board[0][0], board[1][1], board[2][2]);
        if (state != 0) {
            return;
        }
        this.state = judgingSame(board[0][2], board[1][1], board[2][0]);
    }
    private int judgingSame(int a, int b, int c){
        int[] count = new int[3];
        addPowerTenOrOne(a, count);
        addPowerTenOrOne(b, count);
        addPowerTenOrOne(c, count);
        for (int i = 2; i >= 0; i--) {
            if (count[i] >= 3) {
                return i;
            }
        }
        return 0;
    }

    private void addPowerTenOrOne(int data, int[] count) {
        int aTen = data / 10;
        count[aTen]++;
        if (aTen == 0) {
            count[data % 10]++;
        }
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Room room = (Room) o;
        return Objects.equals(id, room.id);
    }

    public Gamer getGamer(String id) {
        for (Gamer gamer : gamers) {
            if (id.equals(gamer.getId())){
                return gamer;
            }
        }
        return null;
    }

    public Gamer getOther(String id) {
        for (Gamer gamer : gamers) {
            if (!id.equals(gamer.getId())){
                return gamer;
            }
        }
        return null;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }



    private static Map<String,Room> TIC_TAC_TOE_ROOM_MAP = new HashMap();
    public static boolean createTicTacToeRoom(String roomId, Room room) {
        if (TIC_TAC_TOE_ROOM_MAP.containsKey(roomId)) {
            return false;
        }
        TIC_TAC_TOE_ROOM_MAP.put(roomId,room);
        return true;
    }

    public static Room getTicTacToeRoom(String roomId) {
        return TIC_TAC_TOE_ROOM_MAP.get(roomId);
    }

    public static Room getRoomByGamer(String gamerId) {
        for (Map.Entry<String, Room> stringRoomEntry : TIC_TAC_TOE_ROOM_MAP.entrySet()) {
            for (Gamer gamer : stringRoomEntry.getValue().gamers) {
                if (gamer != null && gamerId.equals(gamer.getId())){
                    return stringRoomEntry.getValue();
                }
            }
        }
        return null;
    }

    public static void removeRoom(String roomId) {
        TIC_TAC_TOE_ROOM_MAP.remove(roomId);
    }
}
